package com.travel.controller;

import cn.hutool.core.bean.BeanUtil;
import com.travel.context.UserContext;
import com.travel.dto.UserLoginRequestDto;
import com.travel.member.vo.LoginVo;
import com.travel.pojo.Member;
import com.travel.result.PageResult;
import com.travel.result.Result;
import com.travel.service.IMemberService;
import com.travel.vo.MemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: libin
 * Date: 2025/6/9 15:46
 * Description:
 **/
@Api(tags = "小程序端登录接口")
@RestController
@RequestMapping("/travel/member/user")
public class MemberController {


    @Autowired
    private IMemberService memberService;


    @ApiOperation("微信小程序登录")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody UserLoginRequestDto userLoginRequestDto){
        LoginVo loginVo =  memberService.login(userLoginRequestDto);
        return Result.success(loginVo);
    }


    @GetMapping("/getMember")
    public Result<MemberVo> getMember(){
        Integer currentId = UserContext.getCurrentId();
        Member member = memberService.getById(currentId);
        MemberVo memberVo = BeanUtil.copyProperties(member, MemberVo.class);
        return Result.success(memberVo);
    }

    @ApiOperation("获取用户列表")
    @PostMapping("/list")
    public Result<PageResult> list(@RequestParam Integer pageNum,@RequestParam Integer pageSize, @RequestBody(required = false) Member member){
        PageResult memberList = memberService.getUserInfoList(pageNum,pageSize,member);
        return Result.success(memberList);
    }

    @ApiOperation("根据id删除用户信息")
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        memberService.removeById(id);
        return Result.success("删除成功");
    }

    @ApiOperation("根据id修改用户权限")
    @PutMapping("/update/{id}")
    public Result<String> update(@PathVariable Integer id, @RequestBody Member member){
        member.setId(id);
        memberService.updateById(member);
        return Result.success("修改成功");
    }

}
